package com.cadre.common.utils.oldutil;

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/* loaded from: MergePDF.class */
public class MergePDF {
    private static final String MERGE_OUTPUT_FILE = "D:\\001\\001";
    private static final String MERGE_PDF_PATH = "D:\\001\\001";
    private static final Integer EXCEL_TO_PDF_OPERAND = 0;

    public static void main(String[] args) throws Exception {
        FileUtils.createOutputFile("D:\\001\\001");
        mergePdf(pdfFilePath("D:\\001\\001"), "D:\\001\\001");
        System.out.println("合并完成！！！");
    }

    public static void wordToPdf(String fromAddress, String toAddress) {
        ActiveXComponent ax = null;
        Dispatch doc = null;
        try {
            try {
                ComThread.InitSTA();
                long startTime = System.currentTimeMillis();
                ax = new ActiveXComponent("Word.Application");
                ax.setProperty("Visible", false);
                doc = Dispatch.call(ax.getProperty("Documents").toDispatch(), "Open", new Object[]{fromAddress, false, true}).toDispatch();
                File tofile = new File(toAddress);
                if (tofile.exists()) {
                    tofile.delete();
                }
                Dispatch.call(doc, "SaveAs", new Object[]{toAddress, 17});
                System.out.println(fromAddress + "转化完成,总共耗时" + (System.currentTimeMillis() - startTime) + "ms。" + toAddress);
                if (doc != null) {
                    Dispatch.call(doc, "Close", new Object[]{new Variant(false)});
                }
                if (ax != null) {
                    try {
                        ax.invoke("Quit", new Variant[0]);
                    } catch (Exception e) {
                    }
                }
                ComThread.Release();
            } catch (Exception e2) {
                System.out.println("========Error:文档转换失败：" + e2.getMessage());
                if (doc != null) {
                    Dispatch.call(doc, "Close", new Object[]{new Variant(false)});
                }
                if (ax != null) {
                    try {
                        ax.invoke("Quit", new Variant[0]);
                    } catch (Exception e3) {
                        ComThread.Release();
                    }
                }
                ComThread.Release();
            }
        } catch (Throwable th) {
            if (doc != null) {
                Dispatch.call(doc, "Close", new Object[]{new Variant(false)});
            }
            if (ax != null) {
                try {
                    ax.invoke("Quit", new Variant[0]);
                } catch (Exception e4) {
                    ComThread.Release();
                    throw th;
                }
            }
            ComThread.Release();
            throw th;
        }
    }

    public void excel2Pdf(String inFilePath, String outFilePath) throws Exception {
        ActiveXComponent ax = null;
        Dispatch excel = null;
        try {
            try {
                ComThread.InitSTA();
                ax = new ActiveXComponent("Excel.Application");
                ax.setProperty("Visible", new Variant(false));
                ax.setProperty("AutomationSecurity", new Variant(3));
                excel = Dispatch.invoke(ax.getProperty("Workbooks").toDispatch(), "Open", 1, new Object[]{inFilePath, new Variant(false), new Variant(false)}, new int[9]).toDispatch();
                Dispatch.invoke(excel, "ExportAsFixedFormat", 1, new Object[]{new Variant(EXCEL_TO_PDF_OPERAND), outFilePath, new Variant(0)}, new int[1]);
                if (excel != null) {
                    Dispatch.call(excel, "Close", new Object[]{new Variant(false)});
                }
                if (ax != null) {
                    ax.invoke("Quit", new Variant[0]);
                }
                ComThread.Release();
            } catch (Exception es) {
                es.printStackTrace();
                throw es;
            }
        } catch (Throwable th) {
            if (excel != null) {
                Dispatch.call(excel, "Close", new Object[]{new Variant(false)});
            }
            if (ax != null) {
                ax.invoke("Quit", new Variant[0]);
            }
            ComThread.Release();
            throw th;
        }
    }

    public static List<String> pdfFilePath(String filePath) {
        List<String> list = new ArrayList<>();
        for (File f : new File(filePath).listFiles()) {
            list.add(f.getAbsolutePath());
        }
        return list;
    }

    public static void WordToPDF(String startFile, String overFile) throws IOException {
        File inputFile = new File(startFile);
        if (!inputFile.exists()) {
            System.out.println("源文件不存在！");
            return;
        }
        File outputFile = new File(overFile);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().exists();
        }
        Process p = Runtime.getRuntime().exec("C:/Program Files (x86)/OpenOffice 4/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"");
        OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
        connection.connect();
        new OpenOfficeDocumentConverter(connection).convert(inputFile, outputFile);
        connection.disconnect();
        p.destroy();
    }

    public static void mergePdf(List<String> list, String outPath) throws IOException {
        Document document = null;
        PdfCopy copy = null;
        FileOutputStream fos = null;
        try {
            try {
                fos = new FileOutputStream(outPath);
                document = new Document();
                copy = new PdfCopy(document, fos);
                document.open();
                for (String path : list) {
                    System.out.println("world ===" + path);
                    PdfReader reader = new PdfReader(path);
                    int n = reader.getNumberOfPages();
                    for (int i = 1; i < n + 1; i++) {
                        document.newPage();
                        copy.addPage(copy.getImportedPage(reader, i));
                    }
                    if (reader != null) {
                        reader.close();
                    }
                }
                if (document != null) {
                    document.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (copy != null) {
                    copy.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
                if (document != null) {
                    document.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (copy != null) {
                    copy.close();
                }
            }
        } catch (Throwable th) {
            if (document != null) {
                document.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (copy != null) {
                copy.close();
            }
            throw th;
        }
    }
}
